<?php
    /**
    * Demand by subject
    * @author: trungdt
    * @since: Mar 2, 2011
    * @version: 1.0
    */
    if (!defined('EXPONENT')) exit('');

    /**
    * How many items per page
    * WARNING: Must implement better in module config
    */
    $max_item =3;
    // fix bug router
    // Administrator access restrict
    $lang=exponent_lang_loadFile("modules/demandmodule/actions/demand-manage.php");
    $lang2=exponent_lang_loadFile("modules/demandmodule/demand.php");

    // set flow
    exponent_flow_set(SYS_FLOW_PROTECTED,SYS_FLOW_ACTION);

    // check permission
    $loc=null;
    $loc->mod="demandmodule";
    
    // get user id
    if (isset($_REQUEST['first_var']))
    {
        $_REQUEST['subject']=$_REQUEST['first_var'];
    }
    $subject = '';
    if (isset($_REQUEST['subject']))
    {
        $subject=rawurldecode(_ab_safe_html_string($_REQUEST['subject']));
        $subject=str_replace("<","",$subject);
        $subject=str_replace(">","",$subject);
    }
    $subject_id=-1;
    $temp=$db->selectObject("subjects","name = '{$subject}'");
    if ($temp!=null)
    {
        $subject_id=$temp->id;
    }
    
    $show_unapprove=false;
    $show_unapprove_where=" AND (status =2)";
    if (exponent_users_isLoggedIn() && 
    (exponent_permissions_check("administrate",$loc) || 
        exponent_permissions_check("approve_demand",$loc))
    )
    {
        $show_unapprove=true;
        $show_unapprove_where="";
    }
    
    // pagination
    // we count all post
    $count_post = $db->countObjects('demands',"subject_id = {$subject_id} {$show_unapprove_where} ")+$db->countObjects('witness',"subject_id = {$subject_id} AND demand_id = -1 {$show_unapprove_where}");

    // page split
    // cal how many posts
    $pages=floor($count_post / $max_item);
    if ($count_post % $max_item != 0)
        $pages++;

    // get page
    $page=1;
    if (isset($_REQUEST['page']))  $page=intval($_REQUEST['page']);
    if (($page<1) || ($page>$pages)) $page=1;
    $start_pos=($page-1)*$max_item;
    // end of pagination
    $fields_demand="{$db->prefix}demands.id, {$db->prefix}demands.name, {$db->prefix}demands.content, {$db->prefix}demands.user_id, {$db->prefix}demands.username, {$db->prefix}demands.post_date,
    {$db->prefix}demands.`status`, {$db->prefix}demands.unapprove_reason, {$db->prefix}demands.unapprove_username, {$db->prefix}demands.publish_date, {$db->prefix}demands.important_level, 1 `type`, {$db->prefix}subjects.name subject_name, sendmail, pray_count";

    $fields_witness="{$db->prefix}witness.id, {$db->prefix}witness.name, {$db->prefix}witness.content, {$db->prefix}witness.user_id, {$db->prefix}witness.username, {$db->prefix}witness.post_date,
    {$db->prefix}witness.`status`, {$db->prefix}witness.unapprove_reason, {$db->prefix}witness.unapprove_username, {$db->prefix}witness.publish_date, 7 important_level, 2 `type`, {$db->prefix}subjects.name subject_name, 2 sendmail, 0 pray_count ";

    $where="subject_id = {$subject_id} ";  
       
    $data=$db->selectObjectsBySql("
    (SELECT {$fields_demand} 
    FROM  
    {$db->prefix}demands INNER JOIN {$db->prefix}subjects
    ON {$db->prefix}demands.subject_id = {$db->prefix}subjects.id
    WHERE ({$where}) {$show_unapprove_where})

    UNION

    (SELECT {$fields_witness}
    FROM {$db->prefix}witness INNER JOIN {$db->prefix}subjects
    ON {$db->prefix}witness.subject_id = {$db->prefix}subjects.id
    WHERE ({$where}) AND (demand_id = -1) {$show_unapprove_where})

    ORDER BY status ASC, post_date DESC, important_level DESC, publish_date DESC LIMIT {$start_pos},{$max_item}
    ");

    demandmodule::demand_witness_show($data, $lang, $lang2, $show_unapprove);

    $title=$lang2['by-subject-title'];
    $page_action="by_subject";
    $first_var=rawurlencode($subject);

    $title=str_replace("%u",$subject, $title);
    $template=new template("demandmodule","_demand_manage");
    $template->assign("data",$data);
    $template->register_permissions(array('approve_demand',"administrate"),$loc);  
    $template->assign("page",$page);
    $template->assign("first_var",$first_var);
    $template->assign("pages",$pages);
    $template->assign("title",$title);
    $template->assign("page_action",$page_action); 
    $template->assign("start_index",$start_pos);
    $template->assign("demand_manage",0);
    _ab_theme_action_register_title($title." - ".SITE_TITLE);
    if (exponent_users_isLoggedIn())
    {
        $template->assign("logged",1);
    }
    $template->output();
?>
